﻿/**************************************************************************************************\
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++
The Software is protected by US copyright laws and international treaties. Licensee must treat the 
Software like any other copyrighted material – for example a book. Licensee acknowledges that 
Arkadium owns the Software and that nothing set forth herein conveys to Licensee any ownership in 
or to the Software, including without any Derivative Works of the Software.  
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++

AUTHOR: Neil Mrva

PURPOSE: 
This class contains static helper functions that can be accessed anywhere. Generally all 
functions within here don't logically fit into any other classes and can be useful in many projects.
\***************************************************************************************************/

class arkadium.Utility 
{
	
	public function Utility()
	{
		
	}
	
	/**************************************************************************************************\
	getRandom - This function is called when a random whole number needs to be generated and returned.
	\**************************************************************************************************/
	public static function getRandom( nLow:Number, nHigh:Number ):Number
	{
		return Math.floor( ( Math.random() * ( ( nHigh + 1 ) - nLow ) ) + nLow );
	}
	
	// updateLoopingIndex
	public static function updateLoopingIndex( bForward:Boolean, nCurrentPos:Number, nMaxPos:Number ):Number
	{
		if ( nCurrentPos == undefined || nMaxPos == undefined ) return null;
		
		if ( bForward )
		{
			if ( ++nCurrentPos >= nMaxPos )
			{
				return 0;
			}
		}
		else
		{
			if ( --nCurrentPos < 0 )
			{
				return ( nMaxPos - 1 );
			}
		}
		
		return nCurrentPos;
	}
	
	/*function resizeThisTextField(dtField,nXMax,nYMax,nFontMin){
		var exampleTextFormat = dtField.getTextFormat()
		while(dtField.textHeight>nYMax && exampleTextFormat.size>nFontMin){
			exampleTextFormat.size--;
			dtField.setTextFormat(exampleTextFormat);
			//myTxt.autoSize = "left";
		}
	}*/
	
	function resizeThisTextField(dtField,nXMax,nYMax,nFontMin){
        var exampleTextFormat = dtField.getTextFormat()
        while(dtField.textHeight>nYMax && exampleTextFormat.size>nFontMin){
			exampleTextFormat.size--;
			dtField.setTextFormat(exampleTextFormat);
			//myTxt.autoSize = "left";
		}
		trace("resize"+dtField.textWidth);
		while(dtField.textWidth>nXMax && exampleTextFormat.size>nFontMin){
			exampleTextFormat.size--;
			dtField.setTextFormat(exampleTextFormat);
			//myTxt.autoSize = "left";
		}
	}

	/*
	// trim
	public static function trim( sString:String ):String
	{
		var sNewString:String = sString;
		
		// Trim Left
		var nIndexCut:Number = 0;
		while( sString.charCodeAt( nIndexCut ) <= 32 )
		{
			++nIndexCut;
		}
		
		sNewString = sNewString.slice( nIndexCut );
		
		// Trim Right
		nIndexCut = sNewString.length - 1;
		while( sNewString.charCodeAt( nIndexCut ) <= 32 )
		{
			--nIndexCut;
		}
	
		return sNewString.slice( 0, nIndexCut + 1 );
	}
	
	// removeWhite
	public static function removeWhite( sString:String ):String
	{
		var sNewString:String = "";
		var nCharCode:Number = 0;
		
		for ( var i:Number = 0; i < sString.length; ++i )
		{
			nCharCode = sString.charCodeAt( i );
			
			if ( nCharCode > 32 )
			{
				sNewString += String.fromCharCode( nCharCode );
			}
		}
		
		return sNewString;
	}
	
	// checkWhite
	public static function checkWhite( sString:String ):Boolean
	{
		for ( var i:Number = 0; i < sString.length; ++i )
		{
			if ( sString.charCodeAt( i ) <= 32 )
			{
				return true;
			}
		}
		
		return false;
	}
	
	// isEmailAddressValid
	public static function isEmailAddressValid( sEmailAddress:String ):Boolean
	{
		if ( !checkWhite( sEmailAddress ) )
		{
			var pSplitOne:Array = sEmailAddress.split( "@" );
			if ( pSplitOne[ 0 ] && pSplitOne[ 1 ] )
			{
				var pSplitTwo:Array = pSplitOne[ 1 ].split( "." );
				if ( pSplitTwo[ 0 ] && pSplitTwo[ 1 ] )
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	// updateLoopingIndex
	public static function updateLoopingIndex( bForward:Boolean, nCurrentPos:Number, nMaxPos:Number ):Number
	{
		if ( bForward )
		{
			if ( ++nCurrentPos == nMaxPos )
			{
				return 0;
			}
		}
		else
		{
			if ( --nCurrentPos < 0 )
			{
				return ( nMaxPos - 1 );
			}
		}
		
		return nCurrentPos;
	}

	// getRandomIndexArray
	public static function getRandomIndexArray( nLength:Number ):Array
	{
		var nRandomIndicies:Array = new Array( nLength );
		var nIndicies:Array = new Array( nLength );

		for ( var i:Number = 0; i < nLength; ++i )
		{
			nIndicies[ i ] = i;
		}
		
		for ( var i:Number = 0; i < nLength; ++i )
		{
			nRandomIndicies[ i ] = nIndicies.splice( getRandom( 0, nIndicies.length - 1 ), 1 );
		}

		delete nIndicies;
		return nRandomIndicies;		
	}
	
	// toNearestHundredth
	public static function toNearestHundredth( nValue:Number ):Number
	{
		return int( nValue * 100 ) / 100;
	}
	
	// convertNumberStringToArray
	function convertNumberStringToArray( sNumberString:String, nSliceLength:Number, sDelimeter:String ):Array
	{
		// Split the string into elements using the supplied delimeter.
		var pSplitList:Array = sNumberString.split( ( sDelimeter ) ? sDelimeter : "," );	
		
		// Create a new array and if nSliceLength is greater then 1, then the array will become multi-dimensional.
		nSliceLength = ( nSliceLength > 1 ) ? nSliceLength : 1;
		var pNewArray:Array = new Array( pSplitList.length / nSliceLength );
		
		// Loop through the original array and slice it into parts and add them to new array.
		var nSliveIndex:Number = 0;
		for ( var i:Number = 0; i < pNewArray.length; ++i )
		{
			nSliveIndex = i * nSliceLength;
			pNewArray[ i ] = pSplitList.slice( nSliveIndex, nSliveIndex + nSliceLength );
			trace( i + ": " + pNewArray[ i ] );
		}
		
		return pNewArray;	
	}
	
	// getDateStringFromUTC
	public static function getDateStringFromUTC( nUTCTime:Number ):String
	{
		// create a new date by converting UTC time in seconds to milliseconds.
		var pDate:Date = new Date( nUTCTime * 1000 );
		var sDateString:String = "";
		
		// get month
		sDateString += getMonthName( pDate.getMonth() ) + " ";
		
		// get day
		sDateString += pDate.getDate().toString() + ", ";

		// get year
		sDateString += Number( pDate.getYear() + 1900 ).toString();
		
		return sDateString;
	}
	
	// getMonthName
	public static function getMonthName( nMonthNumber:Number ):String
	{
		var pMonthNames:Array =
		[ 
			"January", "February", "March", "April", "May", "June",
			"July", "August", "September", "October", "November", "December"
		];
		
		return pMonthNames[ nMonthNumber ];
	}
	*/

}